package com.meilai.project.service.business.contract.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meilai.project.dto.business.contract.ProxyMediaDetailReq;
import com.meilai.project.entity.business.contract.MediaTypeProxy;
import com.meilai.project.entity.business.contract.ProxyMediaDetail;
import com.meilai.project.entity.business.contract.ProxyMediaDetailExt;
import com.meilai.project.entity.media.MediaType;
import com.meilai.project.mapper.business.contract.MediaTypeProxyMapper;
import com.meilai.project.mapper.business.contract.ProxyMediaDetailMapper;
import com.meilai.project.mapper.business.media.MediaTypeMapper;
import com.meilai.project.service.business.contract.ProxyMediaDetailService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xchen
 * @since 2022-04-13
 */
@Service
public class ProxyMediaDetailServiceImpl extends ServiceImpl<ProxyMediaDetailMapper, ProxyMediaDetail> implements ProxyMediaDetailService {

    @Autowired
    private MediaTypeMapper mediaTypeMapper;

    @Autowired
    private MediaTypeProxyMapper mediaTypeProxyMapper;

    @Override
    public void saveDetails(int type, Long bizId, List<ProxyMediaDetailReq> list) {
        //清理之前填写的
        baseMapper.cleanByBizId(type, bizId);
        if (CollUtil.isEmpty(list)) {
            return;
        }

        List<ProxyMediaDetail> result = new ArrayList<>();
        //处理系统存在的media_type
        List<String> mediaTypeNames = list.stream().map(ProxyMediaDetailReq::getProxy_media_type).collect(Collectors.toList());
        List<MediaType> mediaTypes = mediaTypeMapper.listByNameList(mediaTypeNames);
        Map<String, Long> mediaTypeMap = new HashMap<>();
        mediaTypes.forEach(item -> mediaTypeMap.put(item.getName(), item.getId()));
        List<ProxyMediaDetailReq> remainList = new ArrayList<>();
        for (ProxyMediaDetailReq proxyMediaDetailReq : list) {
            Long mediaTypeId = mediaTypeMap.get(proxyMediaDetailReq.getProxy_media_type());
            if (null == mediaTypeId) {
                remainList.add(proxyMediaDetailReq);
            } else {
                ProxyMediaDetail item = convert(type, bizId, proxyMediaDetailReq);
                item.setMedia_type_category(1);
                item.setMedia_type_id(mediaTypeId);
                result.add(item);
            }
        }

        //处理系统不存在的media_type
        List<String> proxyMediaTypeNameList = remainList.stream().map(ProxyMediaDetailReq::getProxy_media_type)
                .collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(proxyMediaTypeNameList)) {
            //保证所有name都存在
            mediaTypeProxyMapper.batchSaveItemsIfNotExist(proxyMediaTypeNameList);
            List<MediaTypeProxy> mediaTypeProxies = mediaTypeProxyMapper.listByNames(proxyMediaTypeNameList);
            Map<String, Long> mediaTypeProxyMap = new HashMap<>();
            mediaTypeProxies.forEach(item -> mediaTypeProxyMap.put(item.getName(), item.getId()));
            for (ProxyMediaDetailReq proxyMediaDetailReq : remainList) {
                Long mediaTypeId = mediaTypeProxyMap.get(proxyMediaDetailReq.getProxy_media_type());
                ProxyMediaDetail item = convert(type, bizId, proxyMediaDetailReq);
                item.setMedia_type_category(2);
                item.setMedia_type_id(mediaTypeId);
                result.add(item);
            }
        }
        //保存
        saveBatch(result);
    }

    private ProxyMediaDetail convert(int type, Long bizId, ProxyMediaDetailReq detailDTO) {
        ProxyMediaDetail proxyMediaDetail = new ProxyMediaDetail();
        proxyMediaDetail.setType(type);
        proxyMediaDetail.setBiz_id(bizId);
        proxyMediaDetail.setQuantity(detailDTO.getQuantity());
        proxyMediaDetail.setAmount(detailDTO.getAmount());
        proxyMediaDetail.setFill_amount(detailDTO.getFill_amount());
        proxyMediaDetail.setMedia_type_spec(detailDTO.getMedia_type_spec());
        proxyMediaDetail.setDuration(detailDTO.getDuration());
        return proxyMediaDetail;

    }

    @Override
    public List<ProxyMediaDetailReq> query(int type, Long bizId) {
        List<ProxyMediaDetailExt> proxyMediaDetails = baseMapper.queryByBizId(type, bizId);
        if (CollUtil.isEmpty(proxyMediaDetails)) {
            return new ArrayList<>();
        }
        return convert(proxyMediaDetails);
    }

    @Override
    public Map<Long, List<ProxyMediaDetailReq>> queryByIds(int type, List<Long> bizIdList) {
        Map<Long, List<ProxyMediaDetailReq>> result = new HashMap<>();
        if (CollUtil.isEmpty(bizIdList)) {
            return result;
        }
        List<ProxyMediaDetailExt> proxyMediaDetails = baseMapper.queryByBizIdList(type, bizIdList);
        if (CollUtil.isEmpty(proxyMediaDetails)) {
            return result;
        }
        Map<Long, List<ProxyMediaDetailExt>> mediaDetailMap = proxyMediaDetails.stream().collect(Collectors.groupingBy(ProxyMediaDetailExt::getBiz_id));
        for (Map.Entry<Long, List<ProxyMediaDetailExt>> entity : mediaDetailMap.entrySet()) {
            result.put(entity.getKey(), convert(entity.getValue()));
        }
        return result;
    }


    public List<ProxyMediaDetailReq> convert(List<ProxyMediaDetailExt> proxyMediaDetails) {
        List<ProxyMediaDetailReq> result = new ArrayList<>();
        for (ProxyMediaDetailExt proxyMediaDetail : proxyMediaDetails) {
            result.add(convert(proxyMediaDetail));
        }
        return result;
    }

    public ProxyMediaDetailReq convert(ProxyMediaDetailExt proxyMediaDetails) {
        ProxyMediaDetailReq proxyMediaDetailReq = new ProxyMediaDetailReq();
        proxyMediaDetailReq.setQuantity(proxyMediaDetails.getQuantity());
        proxyMediaDetailReq.setAmount(proxyMediaDetails.getAmount());
        proxyMediaDetailReq.setFill_amount(proxyMediaDetails.getFill_amount());
        proxyMediaDetailReq.setProxy_media_type(proxyMediaDetails.getMedia_type_name());
        proxyMediaDetailReq.setMedia_type_spec(proxyMediaDetails.getMedia_type_spec());
        proxyMediaDetailReq.setDuration(proxyMediaDetails.getDuration());
        return proxyMediaDetailReq;
    }

}
